﻿using AutoMapper;
using Decoration.Comm;
using Decoration.IService;
using Decoration.Models;
using Decoration.Models.Dto;
using Decoration.Models.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Decoration.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserProcedureRecordController : ControllerBase
    {
        private readonly IUserProcedureRecordService svc;
        private readonly IUserProcedureService userProcedureSvc;
        private readonly IMapper mapper;

        public UserProcedureRecordController(IUserProcedureRecordService svc,IUserProcedureService userProcedureSvc,IMapper mapper)
        {
            this.svc = svc;
            this.userProcedureSvc = userProcedureSvc;
            this.mapper = mapper;
        }

        [HttpGet("getAll")]
        public Result Get()
        {
            var data = svc.Query(o => true);

            return new Result() { code = 1, msg = "成功", data = data };
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public Result Add(UserProcedureRecordPostDto dto)
        {
            var data = mapper.Map<UserProcedureRecord>(dto);


            // 判断工序是否全部完成
            if(dto.Result == 2)
            {
                var procedure = userProcedureSvc.Get(o=>o.Id == data.UserProcedureId);
                procedure.Status = dto.Result;
                userProcedureSvc.Update(procedure);
            }

            if (svc.Add(data))
                return new Result() { code = 1, msg = "成功" };
            else
                return new Result() { code = -1, msg = "失败" };
        }

        /// <summary>
        /// 根据工序获取记录
        /// </summary>
        /// <param name="userProcedureId"></param>
        /// <returns></returns>
        [HttpGet("GetByProcedure")]
        public Result GetByProcedure(int userProcedureId)
        {
            var data = svc.Query(o=>o.UserProcedureId == userProcedureId);

            return new Result() { code = 1,msg="成功",data = data};
        }
    }
}
